package com.snack.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.snack.shop.common.BusinessException;
import com.snack.shop.entity.SystemConfig;
import com.snack.shop.mapper.SystemConfigMapper;
import com.snack.shop.service.SystemConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 系统配置服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SystemConfigServiceImpl implements SystemConfigService {

    private final SystemConfigMapper systemConfigMapper;

    @Override
    public String getConfigValue(String configKey) {
        LambdaQueryWrapper<SystemConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemConfig::getConfigKey, configKey);
        
        SystemConfig config = systemConfigMapper.selectOne(wrapper);
        return config != null ? config.getConfigValue() : null;
    }

    @Override
    public BigDecimal getConfigValueAsDecimal(String configKey, BigDecimal defaultValue) {
        String value = getConfigValue(configKey);
        if (value == null || value.trim().isEmpty()) {
            return defaultValue;
        }
        
        try {
            return new BigDecimal(value);
        } catch (NumberFormatException e) {
            log.warn("配置值 {} 无法转换为数字，使用默认值 {}", configKey, defaultValue);
            return defaultValue;
        }
    }

    @Override
    public Integer getConfigValueAsInteger(String configKey, Integer defaultValue) {
        String value = getConfigValue(configKey);
        if (value == null || value.trim().isEmpty()) {
            return defaultValue;
        }
        
        try {
            return Integer.valueOf(value);
        } catch (NumberFormatException e) {
            log.warn("配置值 {} 无法转换为整数，使用默认值 {}", configKey, defaultValue);
            return defaultValue;
        }
    }

    @Override
    public Boolean getConfigValueAsBoolean(String configKey, Boolean defaultValue) {
        String value = getConfigValue(configKey);
        if (value == null || value.trim().isEmpty()) {
            return defaultValue;
        }
        
        return "true".equalsIgnoreCase(value) || "1".equals(value);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConfig(String configKey, String configValue) {
        LambdaQueryWrapper<SystemConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemConfig::getConfigKey, configKey);
        
        SystemConfig config = systemConfigMapper.selectOne(wrapper);
        if (config == null) {
            throw new BusinessException("配置不存在：" + configKey);
        }
        
        config.setConfigValue(configValue);
        systemConfigMapper.updateById(config);
    }

    @Override
    public List<SystemConfig> getAllConfigs() {
        return systemConfigMapper.selectList(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdateConfigs(List<SystemConfig> configs) {
        for (SystemConfig config : configs) {
            if (config.getConfigKey() != null && config.getConfigValue() != null) {
                updateConfig(config.getConfigKey(), config.getConfigValue());
            }
        }
    }
}

